package com.endes.android.model.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.endes.android.entidades.SSEC05;
import com.endes.android.model.database.EndesOpenHelper;

public class QSSec5Dao {
	private EndesOpenHelper endesOpenHelper;
	private SQLiteDatabase db;
	private Cursor cursor = null;
	private Context context;

	public QSSec5Dao(Context context) {
		this.context = context;
		endesOpenHelper = new EndesOpenHelper(context);

	}

	public SSEC05 ObtenerSeccion5(int qsCluster, int qsNumber, int qsHome,
			int numeroOrdenPersonaSeleccionada) {
		SSEC05 ssec05 = null;
		db = endesOpenHelper.getReadableDatabase();
		cursor = db
				.rawQuery(
						"Select * from SSEC05 where qsCluster=? and qsNumber=? and qsHome=? and qs20C=?",
						new String[] { qsCluster + "", qsNumber + "",
								qsHome + "",
								numeroOrdenPersonaSeleccionada + "" });
		if (cursor.moveToFirst()) {
			ssec05 = new SSEC05();
			ssec05.setQsCluster(qsCluster);
			ssec05.setQsNumber(qsNumber);
			ssec05.setQsHome(qsHome);
			ssec05.setQs20C(numeroOrdenPersonaSeleccionada);
			ssec05.setQs500(cursor.getInt(cursor.getColumnIndex("qs500")));
			ssec05.setQs501U(cursor.getInt(cursor.getColumnIndex("qs501U")));
			ssec05.setQs501C(cursor.getInt(cursor.getColumnIndex("qs501C")));
			ssec05.setQs503(cursor.getInt(cursor.getColumnIndex("qs503")));
			ssec05.setQs505A(cursor.getInt(cursor.getColumnIndex("qs505A")));
			ssec05.setQs505B(cursor.getInt(cursor.getColumnIndex("qs505B")));
			ssec05.setQs505C(cursor.getInt(cursor.getColumnIndex("qs505C")));
			ssec05.setQs505D(cursor.getInt(cursor.getColumnIndex("qs505D")));
			ssec05.setQs506(cursor.getInt(cursor.getColumnIndex("qs506")));
			ssec05.setQs507A(cursor.getInt(cursor.getColumnIndex("qs507A")));
			ssec05.setQs507B(cursor.getInt(cursor.getColumnIndex("qs507B")));
			ssec05.setQs507C(cursor.getInt(cursor.getColumnIndex("qs507C")));
			ssec05.setQs507D(cursor.getInt(cursor.getColumnIndex("qs507D")));
			ssec05.setQs507E(cursor.getInt(cursor.getColumnIndex("qs507E")));
			ssec05.setQs507F(cursor.getInt(cursor.getColumnIndex("qs507F")));
			ssec05.setQs507G(cursor.getInt(cursor.getColumnIndex("qs507G")));
			ssec05.setQs507H(cursor.getInt(cursor.getColumnIndex("qs507H")));
			ssec05.setQs507I(cursor.getInt(cursor.getColumnIndex("qs507I")));
			ssec05.setQs507J(cursor.getInt(cursor.getColumnIndex("qs507J")));
		}
		Cerrar();
		return ssec05;
	}

	public void guardarSec5Encuesta(SSEC05 ssec05) {
		ContentValues contenido = new ContentValues();
		contenido.put("qs500", ssec05.getQs500());
		contenido.put("qs501U", ssec05.getQs501U());
		contenido.put("qs501C", ssec05.getQs501C());
		if((verificarSexoPersona(ssec05.getQsCluster(), ssec05.getQsNumber(),
				ssec05.getQsHome(), ssec05.getQs20C()) == 1 && verificarEdadPregunta23(ssec05.getQsCluster(), ssec05.getQsNumber(),
				ssec05.getQsHome(), ssec05.getQs20C()) >= 15) || (verificarSexoPersona(ssec05.getQsCluster(), ssec05.getQsNumber(),
				ssec05.getQsHome(), ssec05.getQs20C()) == 2 && verificarEdadPregunta23(ssec05.getQsCluster(), ssec05.getQsNumber(),
				ssec05.getQsHome(), ssec05.getQs20C()) >= 50)){
			contenido.put("qs503", ssec05.getQs503());
			if(ssec05.getQs503() == 1){
				if(verificarMEF(ssec05.getQsCluster(), ssec05.getQsNumber(), ssec05.getQsHome(), ssec05.getQs20C()) == 1){
					contenido.put("qs505A", ssec05.getQs505A());
					contenido.put("qs505B", ssec05.getQs505B());
					contenido.put("qs505C", ssec05.getQs505C());
					contenido.put("qs505D", ssec05.getQs505D());
					contenido.put("qs507A", ssec05.getQs507A());
					contenido.put("qs507B", ssec05.getQs507B());
					contenido.put("qs507C", ssec05.getQs507C());
					contenido.put("qs507D", ssec05.getQs507D());
					contenido.put("qs507E", ssec05.getQs507E());
					contenido.put("qs507F", ssec05.getQs507F());
					contenido.put("qs507G", ssec05.getQs507G());
					contenido.put("qs507H", ssec05.getQs507H());
					contenido.put("qs507I", ssec05.getQs507I());
					contenido.put("qs507J", ssec05.getQs507J());
				} else{
					contenido.put("qs505A", ssec05.getQs505A());
					contenido.put("qs505B", ssec05.getQs505B());
					contenido.put("qs505C", ssec05.getQs505C());
					contenido.put("qs505D", ssec05.getQs505D());
					contenido.put("qs506", ssec05.getQs506());
					contenido.put("qs507A", ssec05.getQs507A());
					contenido.put("qs507B", ssec05.getQs507B());
					contenido.put("qs507C", ssec05.getQs507C());
					contenido.put("qs507D", ssec05.getQs507D());
					contenido.put("qs507E", ssec05.getQs507E());
					contenido.put("qs507F", ssec05.getQs507F());
					contenido.put("qs507G", ssec05.getQs507G());
					contenido.put("qs507H", ssec05.getQs507H());
					contenido.put("qs507I", ssec05.getQs507I());
					contenido.put("qs507J", ssec05.getQs507J());
				}
			} else{
				if(verificarMEF(ssec05.getQsCluster(), ssec05.getQsNumber(), ssec05.getQsHome(), ssec05.getQs20C()) == 2
						|| ssec05.getQs503() == 2 || ssec05.getQs503() == 8){
					contenido.put("qs507A", ssec05.getQs507A());
					contenido.put("qs507B", ssec05.getQs507B());
					contenido.put("qs507C", ssec05.getQs507C());
					contenido.put("qs507D", ssec05.getQs507D());
					contenido.put("qs507E", ssec05.getQs507E());
					contenido.put("qs507F", ssec05.getQs507F());
					contenido.put("qs507G", ssec05.getQs507G());
					contenido.put("qs507H", ssec05.getQs507H());
					contenido.put("qs507I", ssec05.getQs507I());
					contenido.put("qs507J", ssec05.getQs507J());
				}
			} 
		} else{
			if(verificarSexoPersona(ssec05.getQsCluster(), ssec05.getQsNumber(),
					ssec05.getQsHome(), ssec05.getQs20C()) == 2 && verificarEdadPregunta23(ssec05.getQsCluster(), ssec05.getQsNumber(),
					ssec05.getQsHome(), ssec05.getQs20C()) >= 15 && verificarEdadPregunta23(ssec05.getQsCluster(), ssec05.getQsNumber(),
					ssec05.getQsHome(), ssec05.getQs20C()) <= 49){
				if(ssec05.getQs503() == 1){
					if(verificarMEF(ssec05.getQsCluster(), ssec05.getQsNumber(), ssec05.getQsHome(), ssec05.getQs20C()) == 1){
						contenido.put("qs505A", ssec05.getQs505A());
						contenido.put("qs505B", ssec05.getQs505B());
						contenido.put("qs505C", ssec05.getQs505C());
						contenido.put("qs505D", ssec05.getQs505D());
						contenido.put("qs507A", ssec05.getQs507A());
						contenido.put("qs507B", ssec05.getQs507B());
						contenido.put("qs507C", ssec05.getQs507C());
						contenido.put("qs507D", ssec05.getQs507D());
						contenido.put("qs507E", ssec05.getQs507E());
						contenido.put("qs507F", ssec05.getQs507F());
						contenido.put("qs507G", ssec05.getQs507G());
						contenido.put("qs507H", ssec05.getQs507H());
						contenido.put("qs507I", ssec05.getQs507I());
						contenido.put("qs507J", ssec05.getQs507J());
					} else{
						contenido.put("qs505A", ssec05.getQs505A());
						contenido.put("qs505B", ssec05.getQs505B());
						contenido.put("qs505C", ssec05.getQs505C());
						contenido.put("qs505D", ssec05.getQs505D());
						contenido.put("qs506", ssec05.getQs506());
						contenido.put("qs507A", ssec05.getQs507A());
						contenido.put("qs507B", ssec05.getQs507B());
						contenido.put("qs507C", ssec05.getQs507C());
						contenido.put("qs507D", ssec05.getQs507D());
						contenido.put("qs507E", ssec05.getQs507E());
						contenido.put("qs507F", ssec05.getQs507F());
						contenido.put("qs507G", ssec05.getQs507G());
						contenido.put("qs507H", ssec05.getQs507H());
						contenido.put("qs507I", ssec05.getQs507I());
						contenido.put("qs507J", ssec05.getQs507J());
					}
				} else{
					if(verificarMEF(ssec05.getQsCluster(), ssec05.getQsNumber(), ssec05.getQsHome(), ssec05.getQs20C()) == 2
							|| ssec05.getQs503() == 2 || ssec05.getQs503() == 8){
						contenido.put("qs507A", ssec05.getQs507A());
						contenido.put("qs507B", ssec05.getQs507B());
						contenido.put("qs507C", ssec05.getQs507C());
						contenido.put("qs507D", ssec05.getQs507D());
						contenido.put("qs507E", ssec05.getQs507E());
						contenido.put("qs507F", ssec05.getQs507F());
						contenido.put("qs507G", ssec05.getQs507G());
						contenido.put("qs507H", ssec05.getQs507H());
						contenido.put("qs507I", ssec05.getQs507I());
						contenido.put("qs507J", ssec05.getQs507J());
					}
				} 
			}
		}
		if (ExisteSSEC05(ssec05)) {
			db = endesOpenHelper.getWritableDatabase();
			db.update(
					"SSEC05",
					contenido,
					"qsCluster=? and qsNumber=? and qsHome=? and qs20C=?",
					new String[] { ssec05.getQsCluster() + "",
							ssec05.getQsNumber() + "", ssec05.getQsHome() + "",
							ssec05.getQs20C() + "" });
			db.close();
		} else {
			db = endesOpenHelper.getWritableDatabase();
			contenido.put("qsCluster", ssec05.getQsCluster());
			contenido.put("qsNumber", ssec05.getQsNumber());
			contenido.put("qsHome", ssec05.getQsHome());
			contenido.put("qs20C", ssec05.getQs20C());
			db.insert("SSEC05", null, contenido);
			db.close();
		}
	}

	public boolean ExisteSSEC05(SSEC05 ssec05) {
		db = endesOpenHelper.getReadableDatabase();
		cursor = db.rawQuery(
				"Select * from SSEC05 where qsCluster=? and qsNumber=? and qsHome=? "
						+ "and qs20C=?", new String[] {
						ssec05.getQsCluster() + "", ssec05.getQsNumber() + "",
						ssec05.getQsHome() + "", ssec05.getQs20C() + "" });
		if (cursor.moveToFirst()) {
			Cerrar();
			return true;
		} else {
			Cerrar();
			return false;
		}
	}
	
	public int verificarEdadPregunta23(int qsCluster, int qsNumber, int qsHome,
			int numeroOrdenPersonaSeleccionada){
		int edad = 0;
		db = endesOpenHelper.getReadableDatabase();
		cursor = db
				.rawQuery(
						"Select qs23 from SSEC01 where qsCluster=? and qsNumber=? and qsHome=? and qs20C=?",
						new String[] { qsCluster + "", qsNumber + "",
								qsHome + "",
								numeroOrdenPersonaSeleccionada + "" });
		if (cursor.moveToFirst()) {
			edad = cursor.getInt(cursor.getColumnIndex("qs23"));
		}
		return edad;
	}
	
	public int verificarSexoPersona(int qsCluster, int qsNumber, int qsHome,
			int numeroOrdenPersonaSeleccionada){
		int sexo = 0;
		db = endesOpenHelper.getReadableDatabase();
		cursor = db
				.rawQuery(
						"Select qsSexo from SSEC01 where qsCluster=? and qsNumber=? and qsHome=? and qs20C=?",
						new String[] { qsCluster + "", qsNumber + "",
								qsHome + "",
								numeroOrdenPersonaSeleccionada + "" });
		if (cursor.moveToFirst()) {
			sexo = cursor.getInt(cursor.getColumnIndex("qsSexo"));
		}
		return sexo;
	}
	
	public int verificarMEF(int qsCluster, int qsNumber, int qsHome,
			int numeroOrdenPersonaSeleccionada){
		int mef = 0;
		db = endesOpenHelper.getReadableDatabase();
		cursor = db
				.rawQuery(
						"Select qsMEF from SSEC01 where qsCluster=? and qsNumber=? and qsHome=? and qs20C=?",
						new String[] { qsCluster + "", qsNumber + "",
								qsHome + "",
								numeroOrdenPersonaSeleccionada + "" });
		if (cursor.moveToFirst()) {
			mef = cursor.getInt(cursor.getColumnIndex("qsMEF"));
		}
		return mef;
	}

	public void Cerrar() {
		cursor.close();
		db.close();
	}

}
